package jbrowser.security;

import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class RootCallbackHandler implements CallbackHandler {

    private class MultiLineInputPanel extends JPanel {

        private javax.swing.JLabel lblPassword;
        private javax.swing.JPasswordField txtPassword;

        public char[] getPassword() {
            return txtPassword.getPassword();
        }

        public String getUserName() {
            return "root";
        }

        MultiLineInputPanel() {
            java.awt.GridBagConstraints gridBagConstraints;

            lblPassword = new javax.swing.JLabel();
            txtPassword = new javax.swing.JPasswordField();

            setMaximumSize(new java.awt.Dimension(300, 48));
            setMinimumSize(new java.awt.Dimension(300, 48));
            setPreferredSize(new java.awt.Dimension(300, 48));
            setLayout(new java.awt.GridBagLayout());

            lblPassword.setText("Password");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
            add(lblPassword, gridBagConstraints);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.weightx = 0.1;
            gridBagConstraints.weighty = 0.1;
            gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);

            add(txtPassword, gridBagConstraints);
        }
    }

    /**
     * Invoke an array of Callbacks.
     *
     * <p>
     *
     * @param callbacks an array of <code>Callback</code> objects which contain
     * the information requested by an underlying security service to be
     * retrieved or displayed.
     *
     * @exception java.io.IOException if an input or output error occurs.
     * <p>
     *
     * @exception UnsupportedCallbackException if the implementation of this
     * method does not support one or more of the Callbacks specified in * * *
     * the <code>callbacks</code> parameter.
     */
    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        NameCallback nameCallback = null;
        PasswordCallback passwordCallback = null;
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof TextOutputCallback) {
                TextOutputCallback toc = (TextOutputCallback) callbacks[i];
                switch (toc.getMessageType()) {
                    case TextOutputCallback.INFORMATION:
                        JOptionPane.showMessageDialog(null, toc.getMessage(), "Login", JOptionPane.INFORMATION_MESSAGE);
                        break;
                    case TextOutputCallback.ERROR:
                        JOptionPane.showMessageDialog(null, toc.getMessage(), "Login", JOptionPane.ERROR_MESSAGE);
                        break;
                    case TextOutputCallback.WARNING:
                        JOptionPane.showMessageDialog(null, toc.getMessage(), "Login", JOptionPane.WARNING_MESSAGE);
                        break;
                    default:
                        throw new IOException("Unsupported message type: " + toc.getMessageType());
                }
            } else if (callbacks[i] instanceof NameCallback) {
                nameCallback = (NameCallback) callbacks[i];
            } else if (callbacks[i] instanceof PasswordCallback) {
                passwordCallback = (PasswordCallback) callbacks[i];
            } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
            }
        }
        if (nameCallback != null && passwordCallback != null) {
            MultiLineInputPanel inputPanel = new MultiLineInputPanel();
            int result = JOptionPane.showConfirmDialog(null, inputPanel, "Login", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                nameCallback.setName(inputPanel.getUserName());
                passwordCallback.setPassword(inputPanel.getPassword());
            } else if (result == JOptionPane.CANCEL_OPTION) {
                nameCallback.setName(null);
                passwordCallback.setPassword(null);
            }
        }
    }
}